package cn.it.shop.start;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import cn.it.shop.util.GlobalConstants;

/**
 * ClassName: InterfaceUrlInti
 * @Description: 项目启动初始化方法<br/>
 * 加载属性配置文件的键值对到全局变量工具类GlobalConstants中,之后就可用GlobalConstants取出使用
 * @author fangyangsheng
 */
public class InterfaceUrlInti {
	
	 public synchronized static void init(){
		    //获取当前线程的classloader
	        ClassLoader cl = Thread.currentThread().getContextClassLoader();
	        Properties props = new Properties();
	        if(GlobalConstants.interfaceUrlProperties==null){
	            GlobalConstants.interfaceUrlProperties = new Properties();
	        }
	        InputStream in = null;
	        try {
	        	/**
	        	 * getContextClassLoader().getResourceAsStream((String path)
	        	 * 默认则是从ClassPath根下获取，path不能以’/'开头，最终是由ClassLoader获取资源。
	        	 */
	            in = cl.getResourceAsStream("interface_url.properties");
	            props.load(in);//读取流中的键值对
	            for(Object key : props.keySet()){
	            	//存储为全局数据
	                GlobalConstants.interfaceUrlProperties.put(key, props.get(key));
	            }

	            props = new Properties();
	            in = cl.getResourceAsStream("wechat.properties");
	            props.load(in);//读取流中的键值对
	            for(Object key : props.keySet()){
	                GlobalConstants.interfaceUrlProperties.put(key, props.get(key));
	            }

	        } catch (IOException e) {
	            e.printStackTrace();
	        }finally{
	            if(in!=null){
	                try {
	                    in.close();
	                } catch (IOException e) {
	                    e.printStackTrace();
	                }
	            }
	        }
	        return;
	    }
	
	
}
